package com.yeyi.ytest.date;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
    
    
    /**
     * 日期相减
     */
    public static void formatAndParse(){
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Date d1=null;
    	Date d2=null;
    	try {
			d1 = sdf.parse("2017-08-25 21:00:00");
			d2 = sdf.parse("2017-08-25 20:01:10");
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	
    	Date d3Date = new Date(d1.getTime()-d2.getTime());
    	sdf=new SimpleDateFormat("HH:mm:ss");
    	System.out.println(sdf.format(d3Date));
    }
    
    /**
     * 日期加减年
     */
    public static void addCutYear(){
    	SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate = new Date();
		Calendar date = Calendar.getInstance();
		date.setTime(beginDate);
		date.set(Calendar.YEAR, date.get(Calendar.YEAR) - 2);
		try {
			Date endDate = dft.parse(dft.format(date.getTime()));
			System.out.println(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
    }
    
    /**
     * 日期加减日
     */
    public static void addCutDay(){
    	SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate = new Date();
		Calendar date = Calendar.getInstance();
		date.setTime(beginDate);
//		date.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH) - 1);
		date.add(Calendar.DAY_OF_MONTH, -10);
		try {
			Date endDate = dft.parse(dft.format(date.getTime()));
			System.out.println(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
    }
    
    public static void main(String[] args) {
//    	addCutYear();
    	addCutDay();
    }
}